using DG.Tweening;
using UnityEngine;

public class FadeToBlack : MonoBehaviour
{
	private static FadeToBlack instance;

	private static GameObject parentObject;

	[HideInInspector]
	public float alpha;

	private Tweener tweenAlpha;

	private Color initColor;

	private Renderer theRenderer;

	public static FadeToBlack GetInstance()
	{
		if (!instance)
		{
			parentObject = GameObject.Find("FadeToBlack");
			if (parentObject == null)
			{
				parentObject = GameObject.CreatePrimitive(PrimitiveType.Quad);
			}
			instance = parentObject.GetComponent<FadeToBlack>();
		}
		return instance;
	}

	private void Start()
	{
		theRenderer = base.GetComponent<Renderer>();
		initColor = theRenderer.material.GetColor("_Color");
		theRenderer.enabled = false;
	}

	public void FadeIn(float duration, Ease ease = Ease.Linear)
	{
		if (alpha != 1f)
		{
			if (tweenAlpha != null)
			{
				tweenAlpha.Kill();
				tweenAlpha = null;
			}
			tweenAlpha = DOTween.To(() => alpha, delegate(float x)
			{
				alpha = x;
			}, 1f, duration).SetEase(ease).OnUpdate(delegate
			{
				UpdateAlpha();
			});
		}
	}

	public void FadeOut(float duration, Ease ease = Ease.Linear)
	{
		if (alpha != 0f)
		{
			if (tweenAlpha != null)
			{
				tweenAlpha.Kill();
				tweenAlpha = null;
			}
			tweenAlpha = DOTween.To(() => alpha, delegate(float x)
			{
				alpha = x;
			}, 1f, duration).SetEase(ease).OnUpdate(delegate
			{
				UpdateAlpha();
			});
		}
	}

	private void UpdateAlpha()
	{
		if (alpha == 0f)
		{
			base.GetComponent<Renderer>().enabled = false;
			return;
		}
		base.GetComponent<Renderer>().enabled = true;
		Color color = initColor;
		color.a = alpha;
		theRenderer.material.SetColor("_Color", color);
	}

	public void SetAlpha(float alpha_)
	{
		alpha = alpha_;
		UpdateAlpha();
	}
}
